// Kontraband.js for PSPTube
// Made By JK108

function Kontraband_CheckURL( url, option )
{
	if (url.match("http://www.kontraband.co.uk") == "http://www.kontraband.co.uk"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Kontraband URL
*/
function Kontraband_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null){return null; /* failed to get page */}
	var video_url = contents.match(/so.addVariable\(\'file\',encodeURIComponent\(\"(.*?)\"/);
	var contents = GetContents("http://www.kontraband.co.uk" + video_url[1]);
	if(contents == null){return null; /* failed to get page */}
	var pos = contents.indexOf("<location>");
	contents = contents.substring(pos + 10, contents.length);
	var video_url = contents.match(/\<location\>(.*?)\<\/location\>/);
	var video_url = video_url[1];
	return video_url;
}

function Kontraband_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 25;


	result.start = (((result.start - 1) / 10) * perpage) + 1;

	var num = (result.start - 1) / perpage;
	var contents = GetContents("http://www.kontraband.co.uk/search/?search=" + keyword.replace(/ /g, "%20") + "&cf=videos&page=" + num );
	if(contents == null){return null; /* failed to get page */}

	if(contents.match("\t\tYour Search found no match for ") == "\t\tYour Search found no match for "){result.total = 0; return result;}
	var total = contents.match(/SEARCH RESULTS \: (.*?) MATCHES/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<div class=\"listingFillet\" >");

	if(pos != -1){
		contents = contents.substring(pos + 28, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = "http://www.kontraband.co.uk" + url[1].replace(/\n/g, "").substring(0, url[1].length - 5);
		//Get Thumbnail
		var thumb_url = contents.match(/src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1].replace(/\n/g, "");
		//Title
		pos = contents.indexOf("show\" >");
		var title = contents.substring(pos + 7, contents.length);
		pos = title.indexOf("show\" >");
		var title = title.substring(pos + 7, title.length);
		pos = title.indexOf("</a>");
		var title = ImproveText(title.substring(0, pos).replace(/\n/g, ""));
		//Description
		var description = contents.match(/\<div\>(.*?)\<\/div\>/);
		var description = splitTo2Lines(ImproveText(description[1].replace(/\n/g, "")), 60);
		//Comments
		pos = contents.indexOf("show\" >");
		var comments = contents.substring(pos + 7, contents.length);
		pos = comments.indexOf("show\" >");
		var comments = comments.substring(pos + 7, comments.length);
		pos = comments.indexOf("show\" >");
		var comments = comments.substring(pos + 7, comments.length);
		pos = comments.indexOf("</a>");
		var comments = parseInt(comments.substring(0, pos).replace(/\n/g, ""));

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Kontraband = new Object();
Kontraband.Name          = "Kontraband";		/* Service name */
Kontraband.Description   = "Kontraband";		/* Service description */
Kontraband.SearchDesc    = "Kontraband";		/* Service search screen description */
Kontraband.SearchOSKMode = 1;				/* OSK mode 1: English only  2: Japanese */
Kontraband.CheckURL      = Kontraband_CheckURL;		/* CheckURL function */
Kontraband.GetURL        = Kontraband_GetURL;		/* GetURL function */
Kontraband.Search        = Kontraband_Search;		/* Search function */

// add Kontraband to site list
SiteList.push( Kontraband );
